package org.dromara.school.service.impl;

import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.school.domain.bo.TeacherClassesBo;
import org.dromara.school.domain.vo.TeacherClassesVo;
import org.dromara.school.domain.TeacherClasses;
import org.dromara.school.mapper.TeacherClassesMapper;
import org.dromara.school.service.ITeacherClassesService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 教师班级绑定Service业务层处理
 *
 * @author Gjb
 * @date 2025-06-03
 */
@RequiredArgsConstructor
@Service
public class TeacherClassesServiceImpl  extends MPJBaseServiceImpl<TeacherClassesMapper,TeacherClasses> implements ITeacherClassesService {

    private final TeacherClassesMapper baseMapper;

    /**
     * 查询教师班级绑定
     */
    @Override
    public TeacherClassesVo queryById(Long teacherId){
        return baseMapper.selectVoById(teacherId);
    }

    /**
     * 查询教师班级绑定列表
     */
    @Override
    public TableDataInfo<TeacherClassesVo> queryPageList(TeacherClassesBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TeacherClasses> lqw = buildQueryWrapper(bo);
        Page<TeacherClassesVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询教师班级绑定列表
     */
    @Override
    public List<TeacherClassesVo> queryList(TeacherClassesBo bo) {
        LambdaQueryWrapper<TeacherClasses> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TeacherClasses> buildQueryWrapper(TeacherClassesBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TeacherClasses> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTeacherId() != null, TeacherClasses::getTeacherId, bo.getTeacherId());
        lqw.eq(bo.getClassesId() != null, TeacherClasses::getClassesId, bo.getClassesId());
        lqw.eq(bo.getTeacherType() != null, TeacherClasses::getTeacherType, bo.getTeacherType());
        lqw.eq(bo.getSchoolCampusId() != null, TeacherClasses::getSchoolCampusId, bo.getSchoolCampusId());
        return lqw;
    }

    /**
     * 新增教师班级绑定
     */
    @Override
    public Boolean insertByBo(TeacherClassesBo bo) {
        TeacherClasses add = MapstructUtils.convert(bo, TeacherClasses.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setTeacherId(add.getTeacherId());
        }
        return flag;
    }

    /**
     * 修改教师班级绑定
     */
    @Override
    public Boolean updateByBo(TeacherClassesBo bo) {
        TeacherClasses update = MapstructUtils.convert(bo, TeacherClasses.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TeacherClasses entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除教师班级绑定
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<Object> findbyTeacherId(Long userId) {
        MPJLambdaWrapper<TeacherClasses> mpj = new MPJLambdaWrapper<TeacherClasses>();
        mpj.select(TeacherClasses::getClassesId);
        mpj.eq(TeacherClasses::getTeacherId,userId);
        return baseMapper.selectObjs(mpj);
    }

    @Override
    public int deleteByClassId(Long id) {
        MPJLambdaWrapper<TeacherClasses> mpj = new MPJLambdaWrapper<TeacherClasses>();
        mpj.eq(TeacherClasses::getClassesId,id);
        return baseMapper.deleteJoin(mpj);
    }
}
